﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DosciWordPlugin.Common;
using DosciWordPlugin.Models;
using Newtonsoft.Json;

namespace DosciWordPlugin.Controls
{
    public partial class InsertCitation : Form
    {
        public event Func<List<ReferenceModel>, bool, bool> AddToPanelEvent;

        private bool _enableInsert = true;
        public bool EnableInsert
        {
            get
            { 
                return _enableInsert;
            }
            set
            {
                _enableInsert = value;
                btnInsert.Visible = _enableInsert;
            }
        }

        public InsertCitation()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<string, string> dicParam = new Dictionary<string, string>();
                dicParam.Add("keyword", txtSearch.Text.Trim());
                List<ReferenceModel> modelList = HttpUtil.Post<List<ReferenceModel>>(Constant.API_搜索引文, dicParam);

                this.listReference.BeginUpdate();  
                listReference.Items.Clear();
                modelList.ForEach(m => 
                {
                    ListViewItem lvi = new ListViewItem();

                    lvi.Tag = m;
                    lvi.Text = m.Author;
                    lvi.SubItems.Add(m.Year);
                    lvi.SubItems.Add(m.Title);

                    listReference.Items.Add(lvi);
                });
                this.listReference.EndUpdate();
                if (listReference.Items.Count > 0) 
                    listReference.Items[0].Selected = true;
                else
                    SetLabel();
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                WinApp.MsgBoxError(ex.Message);
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                if (listReference.SelectedItems.Count <= 0) 
                {
                    WinApp.MsgBoxInfo("请选择一项引文");
                    return;
                }

                List<ReferenceModel> modelList = new List<ReferenceModel>();
                foreach (ListViewItem lvi in listReference.SelectedItems)
                {
                    ReferenceModel model = lvi.Tag as ReferenceModel;
                    if (model != null) modelList.Add(model);
                }

                if (modelList != null && modelList.Count>0 && AddToPanelEvent != null) 
                {
                    bool suc = AddToPanelEvent(modelList, true);
                    if (suc) WinApp.MsgBoxInfo("插入成功！");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                WinApp.MsgBoxError(ex.Message);
            }
        }

        private void btnAddToPanel_Click(object sender, EventArgs e)
        {
            try
            {
                if (listReference.SelectedItems.Count <= 0)
                {
                    WinApp.MsgBoxInfo("请选择一项引文");
                    return;
                }

                List<ReferenceModel> modelList = new List<ReferenceModel>();
                foreach (ListViewItem lvi in listReference.SelectedItems)
                {
                    ReferenceModel model = lvi.Tag as ReferenceModel;
                    if (model != null) modelList.Add(model);
                }

                if (modelList != null && modelList.Count > 0 && AddToPanelEvent != null)
                {
                    bool suc = AddToPanelEvent(modelList, false);
                    if (suc) WinApp.MsgBoxInfo("加入成功！");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                WinApp.MsgBoxError(ex.Message);
            }
        }

        private void SetLabel(ReferenceModel model=null)
        {
            if (model == null)
            {
                lbType.Text = "";
                lbAuthor.Text = "";
                lbYear.Text = "";
                lbTitle.Text = "";
                lbNumberOfPages.Text = "";
                lbKeywords.Text = "";
                txtDesc.Text = "";
            }
            else {
                lbType.Text = model.Type;
                lbAuthor.Text = model.Author;
                lbYear.Text = model.Year;
                lbTitle.Text = model.Title;
                lbNumberOfPages.Text = model.NumberOfPages;
                lbKeywords.Text = model.Keywords;
                txtDesc.Text = model.Digest;
            }
        }

        private void listReference_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            try
            {
                if (listReference.SelectedItems.Count <= 0)
                {
                    SetLabel();
                    return;
                }
                
                ListViewItem item = listReference.SelectedItems[0];
                ReferenceModel model = item.Tag as ReferenceModel;

                SetLabel(model);
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                WinApp.MsgBoxError(ex.Message);
            }
        }
    }
}
